package com.erp.gather.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.gather.config.UrlConfig;
import com.erp.gather.domain.ErpCollectProductDiagnosisRecord;
import com.ruoyi.gather.api.domain.ErpProductAtlas;
import com.erp.gather.domain.bo.ErpCollectProductDiagnosisRecordBo;
import com.erp.gather.domain.vo.ErpCollectProductDiagnosisRecordVo;
import com.erp.gather.mapper.ErpCollectProductDiagnosisRecordMapper;
import com.erp.gather.mapper.ErpCollectProductMapper;
import com.erp.gather.mapper.ErpProductAtlasMapper;
import com.erp.gather.service.IErpCollectProductDiagnosisRecordService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.ImageMergeUtils;
import com.ruoyi.common.core.utils.JsonUtils;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.gather.api.domain.Aliexpress;
import com.ruoyi.gather.api.domain.ErpCollectProduct;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.dto.OssUrlDto;
import com.ruoyi.system.api.domain.vo.SysOssVo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.map.LinkedMap;
import org.apache.http.entity.ContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.*;
import java.util.*;

/**
 * 在线商品检测记录Service业务层处理
 *
 * @author erp
 * @date 2023-12-12
 */
@RequiredArgsConstructor
@Service
public class ErpCollectProductDiagnosisRecordServiceImpl implements IErpCollectProductDiagnosisRecordService {
    @Autowired
    UrlConfig urlConfig;
    @Resource
    RestTemplate restTemplate;
    @Resource
    RemoteFileService remoteFileService;

    private final AliexpressCategoryServiceImpl aliexpressCategoryService;
    private final ErpCollectProductDiagnosisRecordMapper baseMapper;
    private final ErpCollectProductMapper erpCollectProductMapper;
    private final ErpProductAtlasMapper erpProductAtlasMapper;

    /**
     * 查询在线商品检测记录
     */
    @Override
    public ErpCollectProductDiagnosisRecordVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询在线商品检测记录列表
     */
    @Override
    public TableDataInfo<ErpCollectProductDiagnosisRecordVo> queryPageList(ErpCollectProductDiagnosisRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpCollectProductDiagnosisRecord> lqw = buildQueryWrapper(bo);
        Page<ErpCollectProductDiagnosisRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询在线商品检测记录列表
     */
    @Override
    public List<ErpCollectProductDiagnosisRecordVo> queryList(ErpCollectProductDiagnosisRecordBo bo) {
        LambdaQueryWrapper<ErpCollectProductDiagnosisRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpCollectProductDiagnosisRecord> buildQueryWrapper(ErpCollectProductDiagnosisRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpCollectProductDiagnosisRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCollectProductId() != null, ErpCollectProductDiagnosisRecord::getCollectProductId, bo.getCollectProductId());
        lqw.eq(StringUtils.isNotBlank(bo.getImageURLs()), ErpCollectProductDiagnosisRecord::getImageURLs, bo.getImageURLs());
        lqw.eq(StringUtils.isNotBlank(bo.getSubject()), ErpCollectProductDiagnosisRecord::getSubject, bo.getSubject());
        lqw.eq(StringUtils.isNotBlank(bo.getWhite()), ErpCollectProductDiagnosisRecord::getWhite, bo.getWhite());
        lqw.eq(StringUtils.isNotBlank(bo.getMarketing()), ErpCollectProductDiagnosisRecord::getMarketing, bo.getMarketing());
        lqw.eq(bo.getStatus() != null, ErpCollectProductDiagnosisRecord::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpCollectProductDiagnosisRecord::getRevision, bo.getRevision());
        return lqw;
    }

    /**
     * 新增在线商品检测记录
     */
    @Override
    public Boolean insertByBo(ErpCollectProductDiagnosisRecordBo bo) {
        ErpCollectProductDiagnosisRecord add = BeanUtil.toBean(bo, ErpCollectProductDiagnosisRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改在线商品检测记录
     */
    @Override
    public Boolean updateByBo(ErpCollectProductDiagnosisRecordBo bo) {
        ErpCollectProductDiagnosisRecord update = BeanUtil.toBean(bo, ErpCollectProductDiagnosisRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpCollectProductDiagnosisRecord entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除在线商品检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo<ErpCollectProductDiagnosisRecordVo> graphicProcessingRecordList(String collectProductId, Integer status, PageQuery pageQuery) {
        Page<ErpCollectProductDiagnosisRecordVo> result = baseMapper.graphicProcessingRecordList(pageQuery.build(), collectProductId, status);
        return TableDataInfo.build(result);
    }

    /**
     * 选择主图作为白底图
     *
     * @param imgUrl
     * @param collectProductId
     * @return
     */
    @Override
    public R<Object> chooseMainImageAsWhite(String imgUrl, Long collectProductId) {
        ErpCollectProductDiagnosisRecord collectProductDiagnosisRecord = baseMapper.selectOne(
                new LambdaQueryWrapper<ErpCollectProductDiagnosisRecord>().eq(ErpCollectProductDiagnosisRecord::getCollectProductId, collectProductId)
        );

        if (ObjectUtils.isEmpty(collectProductDiagnosisRecord)) {
            //该信息不存在
            return R.fail("该信息不存在");
        } else {
            //自动抠图
            String handle = urlConfig.getHandle();
            ResponseEntity<String> forEntity = restTemplate.getForEntity(handle + "/medium/remove/background?sourceImgUrl=" + "https://file.pbb2c.com/" + imgUrl, String.class);
            String body = forEntity.getBody();
            JSONObject jsonObject = JSONObject.parseObject(body);
            Integer code = jsonObject.getInteger("code");
            if (200 != code) {
                //改变状态
                // 失败的变为-1
                collectProductDiagnosisRecord.setStatus(-1);
            } else {
                String url = jsonObject.getString("data");
                //TODO OSS 跳过 待测试
                OssUrlDto dto = new OssUrlDto();
                dto.setOldUrl(url);
                R<Object> objectR = remoteFileService.uploadUrl(dto);
                if (objectR.getCode() != 200){
                    return R.fail("上传oss异常");
                }

                String toJSONString = JSONObject.toJSONString(objectR.getData());
                SysOssVo upload = JSONObject.parseObject(toJSONString, SysOssVo.class);
                String fileName = upload.getFileName();

                //进行套图，和白底图标签中的白底图进行套图
                ErpProductAtlas atlas = erpProductAtlasMapper.selectOne(
                        new LambdaQueryWrapper<ErpProductAtlas>()
                                .eq(ErpProductAtlas::getTagId, 1650776561517563905L)
                );
                if (ObjectUtils.isEmpty(atlas)) {
                    return R.fail("白底图背景为空");
                }
                String backgroundPic = "https://file.pbb2c.com/" + atlas.getPictureUrl();
                String elementPic = "https://file.pbb2c.com/" + fileName;
                String newPicUrl = urlConfig.getTempPicPath() + "" + IdUtil.fastSimpleUUID() + ".jpg";
                ImageMergeUtils.overlyingImageTest(backgroundPic, elementPic, newPicUrl);
                String mergeUrl = uploadNewPicToOss(newPicUrl);
                System.out.println(mergeUrl);
                //改变状态
                //变为1 待确认是否应用白底
                collectProductDiagnosisRecord.setStatus(1);
                collectProductDiagnosisRecord.setWhite(mergeUrl);
            }
            //保存图片
            if (baseMapper.updateById(collectProductDiagnosisRecord) > 0) {
                if (collectProductDiagnosisRecord.getStatus() == -1) {
                    return R.fail("白底图抠图异常");
                } else {
                    return R.ok("请确认生成的白底图");
                }
            } else {
                return R.fail("保存失败");
            }

        }
    }

    @Override
    public R<Object> confirmAllUseWhite() {
        //获取是1的
        List<ErpCollectProductDiagnosisRecord> records = baseMapper.selectList(
                new LambdaQueryWrapper<ErpCollectProductDiagnosisRecord>()
                        .eq(ErpCollectProductDiagnosisRecord::getStatus, 1)
        );
        //循环作图
        for (ErpCollectProductDiagnosisRecord record : records) {
            confirmUseWhite(record.getCollectProductId());
        }
        return R.ok("共计做完" + records.size() + "张图");
    }

    /**
     * 确认使用白底图
     *
     * @param collectProductId
     * @return
     */
    @Override
    public R<Object> confirmUseWhite(Long collectProductId) {
        ErpCollectProductDiagnosisRecord collectProductDiagnosisRecord = baseMapper.selectOne(
                new LambdaQueryWrapper<ErpCollectProductDiagnosisRecord>().eq(ErpCollectProductDiagnosisRecord::getCollectProductId, collectProductId)
        );

        if (ObjectUtils.isEmpty(collectProductDiagnosisRecord)) {
            //该信息不存在
            return R.fail("该信息不存在");
        } else {
            ErpCollectProduct collectProduct = erpCollectProductMapper.selectOne(
                    new LambdaQueryWrapper<ErpCollectProduct>()
                            .eq(ErpCollectProduct::getId, collectProductId)
            );
            if (ObjectUtils.isEmpty(collectProduct)) {
                return R.fail("商品不存在");
            } else {
//                合成营销图
//                1.获取背景图库中的任意一张
                ErpProductAtlas atlas = erpProductAtlasMapper.selectOne(
                        new LambdaQueryWrapper<ErpProductAtlas>()
                                .eq(ErpProductAtlas::getTagId, "1734451666229985281")
//                                .last("ORDER BY RAND() LIMIT 1")
                );
                //2.合成图片
                if (ObjectUtils.isEmpty(atlas)) {
                    return R.fail("背景库中无可用图片");
                }
                String backgroundPic = "https://file.pbb2c.com/" + atlas.getPictureUrl();
                String elementPic = "https://file.pbb2c.com/" + collectProductDiagnosisRecord.getWhite();
                String newPicUrl = urlConfig.getTempPicPath() + "" + IdUtil.fastSimpleUUID() + ".jpg";

                ImageMergeUtils.overlyingImageTest(backgroundPic, elementPic, newPicUrl);
                // 将临时图片上传OSS并获取到最新路径并删除临时图片
                String mergeUrl = uploadNewPicToOss(newPicUrl);
                System.out.println(mergeUrl);
                //3.保存图片至collectProductDiagnosisRecord 和 collectProduct
                collectProductDiagnosisRecord.setMarketing(mergeUrl);
                collectProduct.setMarketing(mergeUrl);
                collectProduct.setWhite(collectProductDiagnosisRecord.getWhite());
                //4/确认修改
                collectProductDiagnosisRecord.setStatus(2);
                baseMapper.updateById(collectProductDiagnosisRecord);
                erpCollectProductMapper.updateById(collectProduct);

                return R.ok("确认保存完成，请确认营销图");
            }
        }
    }

    private String uploadNewPicToOss(String newPicUrl) {
        File file = new File(newPicUrl);
        String name = file.getName();
        InputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        MultipartFile multipartFile = null;
        try {
            multipartFile = new MockMultipartFile(name, name, ContentType.APPLICATION_OCTET_STREAM.toString(),
                    fileInputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        //TODO OSS跳过 待测试
        R<Object> objectR = remoteFileService.uploadOss(multipartFile);
        if (objectR.getCode() != 200){
            return "上传oss失败";
        }

        String toJSONString = JSONObject.toJSONString(objectR.getData());
        SysOssVo upload = JSONObject.parseObject(toJSONString, SysOssVo.class);
//        SysOssVo upload = sysOssService.upload(multipartFile);
        // 删除临时图片
        file.delete();
        return upload.getUrl();
    }

    //获取品牌
    public JSONObject getBrand(JSONObject product, String storeId) {
        //0、检测品牌，没有品牌的，需要随机选品牌，如果没有品牌，就不进行修改
        JSONArray propertyList = JSONArray.parseArray(product.getJSONObject("aeop_ae_product_propertys").getString("aeop_ae_product_property"));
        Boolean brandFlag = true;
        for (int i = 0; i < propertyList.size(); i++) {
            JSONObject property = propertyList.getJSONObject(i);
            Integer attrNameId = property.getInteger("attr_name_id");
            if (attrNameId == 2) {
                brandFlag = false;
                break;
            }
        }
        if (brandFlag) {
            List<Aliexpress.ValueVo> brandById = aliexpressCategoryService.getBrandById(product.getLong("category_id"), Long.valueOf(storeId));
            if (brandById == null || brandById.size() == 0)
                return null;
            Aliexpress.ValueVo valueVo = brandById.get(0);
            String brandStr = "[{\"attr_name\":\"" + valueVo.getAttr_value() + "\",\"attr_name_id\":2,\"attr_value_id\":"
                    + valueVo.getAttr_value_id() + "},";
            String customAttributes = product.getJSONObject("aeop_ae_product_propertys").getString("aeop_ae_product_property");
            customAttributes = customAttributes.replaceFirst("\\[", brandStr);
            JSONArray customAttributesJson = JSONArray.parseArray(customAttributes);
            product.put("aeop_ae_product_propertys", customAttributesJson);
        }

        return product;
    }

    //TODO 商品不良信息 暂不使用
//    @Override
//    public R<Object> processingQualificationInfo(JSONObject product, String storeId) {
//        JSONObject brand = getBrand(product, storeId);
//        if (ObjectUtils.isEmpty(brand)) {
//            return R.fail("没有合适品牌");
//        }
//        product = brand;
////        1、自动抠图，放入白底图
//        //选择主图中的一张
//        List<String> list = Arrays.asList(product.getString("image_u_r_ls").split(";"));
//        String mainImage = list.get(0);
////        String mainImageOss = uploadNewPicToOss(mainImage);
//        //自动抠图
//        String handle = urlConfig.getHandle();
//        ResponseEntity<String> forEntity = restTemplate.getForEntity(handle + "/medium/remove/background?sourceImgUrl=" + mainImage, String.class);
//        String body = forEntity.getBody();
//        JSONObject jsonObject = JSONObject.parseObject(body);
//        Integer code = jsonObject.getInteger("code");
//        if (code != 200) {
//            //记录抠图失败
//            return R.fail("抠图失败");
//        }
//        String url = jsonObject.getString("data");
//        SysOssVo upload = sysOssService.upload(url);
//        String fileName = upload.getFileName();
//
//        //进行套图，和白底图标签中的白底图进行套图
//        ErpProductAtlas atlas = erpProductAtlasMapper.selectOne(
//                new LambdaQueryWrapper<ErpProductAtlas>()
//                        .eq(ErpProductAtlas::getTagId, 1650776561517563905L)
//        );
//        if (ObjectUtils.isEmpty(atlas)) {
//            return R.fail("白底图背景为空");
//        }
//        String backgroundWhitePic = "https://file.pbb2c.com/" + atlas.getPictureUrl();
//        String elementWhitePic = "https://file.pbb2c.com/" + fileName;
//        String newWhitePicUrl = tempPicPath + "" + IdUtil.fastSimpleUUID() + ".jpg";
//        ImageMergeUtils.overlyingImageTest(backgroundWhitePic, elementWhitePic, newWhitePicUrl);
//        String mergeWhiteUrl = uploadNewPicToOss(newWhitePicUrl);
//        System.out.println(mergeWhiteUrl);
//
//
////        2、白底图组合生成营销图
////                1.获取背景图库中的任意一张
//        ErpProductAtlas atlasMarketing = erpProductAtlasMapper.selectOne(
//                new LambdaQueryWrapper<ErpProductAtlas>()
//                        .eq(ErpProductAtlas::getTagId, "1734451666229985281")
////                                .last("ORDER BY RAND() LIMIT 1")
//        );
//        //2.合成图片
//        if (ObjectUtils.isEmpty(atlasMarketing)) {
//            return R.fail("背景库中无可用图片");
//        }
//        String backgroundMarketingPic = "https://file.pbb2c.com/" + atlasMarketing.getPictureUrl();
//        String elementMarketingPic = "https://file.pbb2c.com/" + mergeWhiteUrl;
//        String newPicMarketingUrl = tempPicPath + "" + IdUtil.fastSimpleUUID() + ".jpg";
//
//        ImageMergeUtils.overlyingImageTest(backgroundMarketingPic, elementMarketingPic, newPicMarketingUrl);
//        // 将临时图片上传OSS并获取到最新路径并删除临时图片
//        String mergeMarketingUrl = uploadNewPicToOss(newPicMarketingUrl);
//        System.out.println(mergeMarketingUrl);
//
//        //上传图片银行
////        String storeUserName = erpSellerStoreEmpowerMapper.selectById(storeId).getStoreUserName();
//        String storeUserName = null;
//        ImageBankParam ImageBankParam = new ImageBankParam();
//        ImageBankParam.setStoreUserName(storeUserName);
//        ImageBankParam.setGroupId("0");
//        ImageBankParam.setSellerId(Long.valueOf(storeId));
//        //上传白底图至图片银行
//        ImageBankParam.setImageUrl(mergeWhiteUrl);
//
//        forEntity = restTemplate
//                .postForEntity(aeConfig.getEndpoint() + "/erp/erpProduct/uploadImageForSDKTest", ImageBankParam, String.class);
//        body = (String) forEntity.getBody();
//        ImageBankParam returnParam = JSONObject.parseObject(body, ImageBankParam.class);
//        mergeWhiteUrl = returnParam.getNewDraw();
//
//        //上传营销图至图片银行
//        ImageBankParam.setImageUrl(mergeMarketingUrl);
//        forEntity = restTemplate
//                .postForEntity(aeConfig.getEndpoint() + "/erp/erpProduct/uploadImageForSDKTest", ImageBankParam, String.class);
//        body = (String) forEntity.getBody();
//        returnParam = JSONObject.parseObject(body, ImageBankParam.class);
//        mergeMarketingUrl = returnParam.getNewDraw();
//
//        JSONArray imageJsonArr = new JSONArray();
//        JSONObject marketJson = new JSONObject();
//        marketJson.put("url", mergeMarketingUrl);
//        marketJson.put("image_type", 1);
//        JSONObject whiteJson = new JSONObject();
//        whiteJson.put("url", mergeWhiteUrl);
//        whiteJson.put("image_type", 2);
//        imageJsonArr.add(marketJson);
//        imageJsonArr.add(whiteJson);
//        product.put("market_images", imageJsonArr);
//
//        return R.ok(product);
//    }

    //TODO 处理应用程序详细信息 暂不使用
//    @Override
//    public R<Object> processingAppDetailInfo(JSONObject product, String storeId) {
//        JSONObject brand = getBrand(product, storeId);
//        if (ObjectUtils.isEmpty(brand)) {
//            return R.fail("没有合适品牌");
//        }
//        product = brand;
//
//        //生成新的app详描
//        JSONObject detailSourceList = product.getJSONObject("detail_source_list");
//        System.out.println(detailSourceList);
//        JSONArray detailSourceArr = JSONArray.parseArray(detailSourceList.getString("detail_source"));
//        JSONArray detailSourceNewArr = new JSONArray();
//        for (int i = 0; i < detailSourceArr.size(); i++) {
//            JSONObject detailSource = detailSourceArr.getJSONObject(i);
//            JSONObject mobileDetail = detailSource.getJSONObject("mobile_detail");
//            if (ObjectUtils.isNotEmpty(mobileDetail)) {
//                return R.fail("已有移动端详情");
//            }
//
//            JSONObject webDetail = detailSource.getJSONObject("web_detail");
//            System.out.println(webDetail);
//            if (ObjectUtils.isEmpty(webDetail)) {
//                return R.fail("商品没有详描");
//            }
//
//
//            String html = webDetail.getString("moduleList");
//            Document doc = Jsoup.parse(html);
//            Elements paragraphs = doc.select("p:not(:has(img))");
//            StringBuffer makeDetailBuffer = new StringBuffer();
//            for (Element paragraph : paragraphs) {
//                System.out.println(paragraph.text());
//                String replace = paragraph.text().replace("/<p>", "");
//                replace = replace.replace("<p>", "");
//                makeDetailBuffer.append(replace);
//            }
//            System.out.println(makeDetailBuffer.toString());
//
//            html = webDetail.getString("moduleList");
//            doc = Jsoup.parse(html);
//            paragraphs = doc.select("img");
//            List<String> imgArr = new ArrayList<>();
//            for (Element imgElement : paragraphs) {
//                System.out.println(imgElement.attr("src").replace("\"", "").replace("\\", ""));
//                imgArr.add(imgElement.attr("src").replace("\"", "").replace("\\", ""));
//            }
//            System.out.println(imgArr);
//            String imgStr = imgArr.stream()
//                    .map(String::trim) // 移除每个URL前后的空格
//                    .collect(Collectors.joining(";"));
//
//            String makeDetail = makeDetail(makeDetailBuffer.toString(), imgStr);
//            System.out.println(makeDetail);
//            detailSource.put("mobile_detail", makeDetail);
//            detailSourceNewArr.add(detailSource);
//        }
//
//        detailSourceList.put("detail_source", detailSourceNewArr);
//        product.put("detail_source_list", detailSourceList);
//        return R.ok(product);
//    }

    /**
     * 商品详描处理
     *
     * @param detail
     * @return
     */
    public String makeDetail(String detail, String imgUrls) {
        Map<String, Object> map = new HashMap<>();
        map.put("version", "2.0.0");
        Map<String, Object> map1 = new LinkedMap<String, Object>();
        map1.put("type", "text-image");

        // 文本
        List<Object> list = new ArrayList<>();
        if (StringUtils.isNotEmpty(detail)) {
            String[] split = detail.split("\n");
            for (String string : split) {
                Map<String, Object> map2 = new HashMap<String, Object>();
                map2.put("content", string);
                map2.put("class", "body");
                list.add(map2);
            }
        }

        // 图片
        List<Object> list2 = new LinkedList<>();
        String[] split2 = imgUrls.split(";");
        for (String string : split2) {
            Map<String, Object> map2 = new HashMap<String, Object>();
            map2.put("url", string);
            map2.put("style", new HashMap<>());
            list2.add(map2);
        }

        if (list != null && list.size() != 0)
            map1.put("texts", list);
        if (list2 != null && list2.size() != 0)
            map1.put("images", list2);
        List<Object> resList = new ArrayList<Object>();
        resList.add(map1);
        map.put("moduleList", resList);
        return JsonUtils.toJsonString(map);
    }
}
